//
//  CLFileInfo.m
//  FileSystemDemo
//
//  Created by 小草毋正 on 2018/1/6.
//  Copyright © 2018年 C-LOVE. All rights reserved.
//

#import "CLFileInfo.h"

@implementation CLFileInfo

- (instancetype)initWithFilePath:(NSString *)filePath {
    self = [super init];
    if (self) {
        if(![self analysisFileInfoWithPath:filePath]){
            return nil;
        }
        self.filePath = filePath;
    }
    return self;
}

- (BOOL)analysisFileInfoWithPath:(NSString *)filePath {
    if(filePath == nil){
        return NO;
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePath error:&error];
    if(error){
        return NO;
    }
    NSArray *allKeys = [fileAttributes allKeys];
    if([allKeys containsObject:NSFileCreationDate]){
        self.creatDate = [fileAttributes valueForKeyPath:NSFileCreationDate];
    }
    if([allKeys containsObject:NSFileExtensionHidden]){
        self.extensionHidden = [[fileAttributes valueForKey:NSFileExtensionHidden] boolValue];
    }
    if([allKeys containsObject:NSFileGroupOwnerAccountID]){
        self.groupOwnerAccountID = [fileAttributes valueForKeyPath:NSFileGroupOwnerAccountID];
    }
    if([allKeys containsObject:NSFileGroupOwnerAccountName]){
        self.groupOwnerAccountName = [fileAttributes valueForKeyPath:NSFileGroupOwnerAccountName];
    }
    if([allKeys containsObject:NSFileHFSCreatorCode]){
        self.hfsCreatorCode = [[fileAttributes valueForKeyPath:NSFileHFSCreatorCode] unsignedIntValue];
    }
    if([allKeys containsObject:NSFileHFSTypeCode]){
        self.hfsTypeCode = [[fileAttributes valueForKeyPath:NSFileHFSTypeCode] unsignedIntValue];
    }
    if([allKeys containsObject:NSFileImmutable]){
        self.immutable = [[fileAttributes valueForKey:NSFileImmutable] boolValue];
    }
    if([allKeys containsObject:NSFileModificationDate]){
        self.modificationDate = [fileAttributes valueForKeyPath:NSFileModificationDate];
    }
    if([allKeys containsObject:NSFileOwnerAccountID]){
        self.ownerAccountID = [fileAttributes valueForKeyPath:NSFileOwnerAccountID];
    }
    if([allKeys containsObject:NSFileOwnerAccountName]){
        self.ownerAccountName = [fileAttributes valueForKeyPath:NSFileOwnerAccountName];
    }
    if([allKeys containsObject:NSFilePosixPermissions]){
        self.permissions = [[fileAttributes valueForKeyPath:NSFilePosixPermissions] integerValue];
    }
    if([allKeys containsObject:NSFileSize]){
        self.size = [[fileAttributes valueForKeyPath:NSFileSize] unsignedLongLongValue];
    }
    if([allKeys containsObject:NSFileSystemFileNumber]){
        self.fileNumber = [[fileAttributes valueForKeyPath:NSFileSystemFileNumber] integerValue];
    }
    if([allKeys containsObject:NSFileSystemNumber]){
        self.systemNumber = [[fileAttributes valueForKeyPath:NSFileSystemNumber] integerValue];
    }
    if([allKeys containsObject:NSFileType]){
        self.fileType = [fileAttributes valueForKeyPath:NSFileType];
    }
    return YES;
} 


@end




